#pragma once

#include <QTableView>
#include <QHeaderView>
#include <QScroller>
#include <QScrollBar>
#include <QAbstractItemView>

namespace cloud
{

class Table : public QTableView
{
public:
    Table(QWidget* parent = nullptr) : QTableView(parent)
    {
        setTable();
    }

    void setTable()
    {
        // Set dimensions and display dividing lines
        QTableView::setShowGrid(true);
        QTableView::setGeometry(0, 0, 800, 480);

        QTableView::verticalHeader()->setVisible(false);
        // set header line height
        QTableView::horizontalHeader()->setFixedHeight(50);
        // Set the content is readonly
        QTableView::setEditTriggers(QAbstractItemView::NoEditTriggers);
        // Click to select only one row
        QTableView::setSelectionBehavior(QAbstractItemView::SelectRows);
        // Hidden the scrollbar
        QTableView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        QTableView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        // Make sliding smoother
        QTableView::setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
        // Set min line height
		QTableView::verticalHeader()->setDefaultSectionSize(80);

        // Register gesture recognizer for the scroll area, set to TouchGesture on embedded devices
        QScroller* scroller = QScroller::scroller(QTableView::viewport());
        if (QSysInfo::productType() == "embedded")
            scroller->grabGesture(QTableView::viewport(), QScroller::TouchGesture);
        else
            scroller->grabGesture(QTableView::viewport(), QScroller::LeftMouseButtonGesture);
    }

    void setColumsWidth(int r0, int r1, int r2, int r3)
    {
        int w0 = 800 / 100 * r0;
        int w1 = 800 / 100 * r1;
        int w2 = 800 / 100 * r2;
        int w3 = 800 / 100 * r3;

        QTableView::setColumnWidth(0, w0);
        QTableView::setColumnWidth(1, w1);
        QTableView::setColumnWidth(2, w2);
        QTableView::setColumnWidth(3, w3);
    }
};

}
